<template>
  <el-table :data="productAttrTableData" border style="width: 100%">
    <el-table-column align="center" prop="id" label="编号"> </el-table-column>
    <el-table-column align="center" prop="name" label="类型名称" width="250">
    </el-table-column>
    <el-table-column align="center" prop="attributeCount" label="属性数量">
    </el-table-column>
    <el-table-column align="center" prop="paramCount" label="参数数量">
    </el-table-column>
    <el-table-column align="center" label="设置" width="200px">
      <template #default="{ row }">
        <div class="setting">
          <span>
            <el-button size="small" @click="attrListFn(row.id, row.name, type)">
              属性列表
            </el-button>
          </span>
          <span
            ><el-button size="small" @click="argumentListFn(row.id, row.name)"
              >参数列表</el-button
            ></span
          >
        </div>
      </template>
    </el-table-column>
    <el-table-column align="center" label="操作">
      <template #default="{ row }">
        <div>
          <p><el-button size="small">编辑</el-button></p>
          <p>
            <el-button size="small" type="danger" @click="delProCate(row.id)"
              >删除</el-button
            >
          </p>
        </div>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页  -->
  <div class="sorter">
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 点击删除组件 -->
  <deleteProductAttr
    :showDelGoods="showDelGoods"
    @hidden="hiddenDel"
  ></deleteProductAttr>
</template>

<script lang="ts" setup>
import { reactive, toRefs, ref } from "vue";
import * as http from "../../request/http";
import { useRouter } from "vue-router";
import deleteProductAttr from "./components/productDel.vue";

// 商品数据类型列表
const productAttrTableData = ref([]);

// 请求接口获取数据 获取商品类型数据
async function fetchData(pageNum: number = 1, pageSize: number = 5) {
  let res = await http.productAttributeList({
    pageNum,
    pageSize,
  });

  if (res.code === 200) {
    productAttrTableData.value = res.data.list;
    total.value = res.data.total;
  }
}
fetchData();

// 分页 组件
let currentPage = ref(1);
let pageSize = ref(5);
let total = ref(0);
// 条数
const handleSizeChange = (size: number) => {
  console.log(size);
  fetchData(currentPage.value, size);
};
// 页码
const handleCurrentChange = (curPage: number) => {
  fetchData(curPage, pageSize.value);
};

// 点击属性列表
let type = ref(0);
const router = useRouter();
const attrListFn = (id: number, name: string, type: number) => {
  router.push({
    path: "productAttrList",
    query: { id, name, type },
  });
};

// 参数列表
function argumentListFn(id: number, name: string) {
  type.value = 1;

  attrListFn(id, name, 1);
  console.log(111);
}

// 点击删除
let showDelGoods = ref(false);
let ids = ref(0);

function delProCate(id: number) {
  // console.log("这是id", id);
  // console.log("这是del", delStatus);
  showDelGoods.value = true;
  ids.value = id;
}

// 子组件点击取消 隐藏删除弹框
async function hiddenDel(flag?: string) {
  showDelGoods.value = false;
  if (flag === "reloadDel") {
    let res = await http.productAttributeDel(ids.value);
    // console.log("删除", res);
    if (res.code === 200) {
      fetchData(currentPage.value, pageSize.value);
    }
  }
}
</script>
<style lang="less" scoped>
.sorter {
  float: right;
  margin-top: 10px;
  margin-right: 10px;
}

.setting {
  display: flex;
  :nth-child(1) {
    margin-right: 5px;
  }
}
</style>
